package com.zx.mes.response;

import java.util.List;

/**
 * table封装(进行重构)
 *
 * @author hyl
 * @date 2017-12-16
 */
public class TableResultResponse<T> extends BaseResponse {

    private TableData<T> data = new TableData<>();


    public TableData<T> getData() {
        return data;
    }

    public void setData(TableData<T> data) {
        this.data = data;
    }


    public TableResultResponse() {
        this.data = new TableData<>();
    }

    public TableResultResponse(TableData<T> data) {
        this.data = data;
    }


    /**
     * 常用
     *
     * @param total
     * @param rows
     */
    @Deprecated
    public TableResultResponse(long total, List<T> rows) {
        this.data = new TableData<>(total, rows);
    }

    @Deprecated
    public TableResultResponse(long total, long page, List<T> rows) {
        this.data = new TableData<>(total, page, rows);
    }

    /**
     * 常用
     *
     * @param total
     * @param rows
     */
    @Deprecated
    public TableResultResponse(String message, Integer status, long total, List<T> rows) {
        super(status, message);
        this.data = new TableData<>(total, rows);
    }

    @Deprecated
    public TableResultResponse(String message, Integer status, long total, long page, List<T> rows) {
        super(status, message);
        this.data = new TableData<>(total, page, rows);
    }

    /**
     * 内部类
     *
     * @param <T>
     */
    class TableData<T> {
        private long total;

        private long page;
        private List<T> rows;

        public TableData() {
        }

        public TableData(long total, List<T> rows) {
            this.total = total;
            this.rows = rows;
        }

        public TableData(long total, long page, List<T> rows) {
            this(total, rows);
            this.page = page;
        }

        public long getTotal() {
            return total;
        }

        public void setTotal(long total) {
            this.total = total;
        }

        public List<T> getRows() {
            return rows;
        }

        public void setRows(List<T> rows) {
            this.rows = rows;
        }

        public long getPage() {
            return page;
        }

        public void setPage(long page) {
            this.page = page;
        }
    }

    public static class Builder<T> {

        private TableResultResponse<T> response = new TableResultResponse<>();

        public Builder() {

        }

        public Builder status(Integer status) {
            response.setStatus(status);
            return this;
        }

        public Builder message(String message) {
            response.setMessage(message);
            return this;
        }

        public Builder total(long total) {
            response.getData().setTotal(total);
            return this;
        }

        public Builder page(long page) {
            response.getData().setPage(page);
            return this;
        }

        public Builder<T> rows(List<T> rows) {
            response.getData().setRows(rows);
            return this;
        }

        public TableResultResponse<T> build() {
            return response;
        }

    }
}
